import Vue from 'vue'
import VueRouter from 'vue-router'
import LoginView from '../views/sys/LoginView.vue'
import RegisterView from "@/views/sys/RegisterView";
import HomeView from "@/views/home/HomeView";
import UserInfoView from "@/views/info/UserInfoView";
import MonitorView from "@/views/monitor/MonitorView";
import ModifyInfoView from "@/views/info/ModifyInfoView";
import MonitorAreaView from "@/views/monitor/MonitorAreaView";
import AddOldView from "@/views/addInfo/addOld";
import AddWorkerView from "@/views/addInfo/addWorker";
import LogView from "@/views/record/LogView";
import MonitorDetail from "@/views/monitor/MonitorDetail";
import TraceView from "@/views/record/TraceView";

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'LoginView',
    meta: {
      roles: ['manager', 'user']
    },
    component: LoginView
  },
  {
    path: "/register",
    name: "RegisterView",
    meta: {
      roles: ['manager', 'user']
    },
    component: RegisterView
  },

  {
    path: '/home',
    name: 'HomeView',
    meta: {
      roles: ['manager', 'user']
    },
    component: HomeView,
    children:[
      {
        path:'userInfo',
        name: 'UserInfoView',
        meta: {
          roles: ['manager', 'user']
        },
        component:UserInfoView
      },
      {
        path: 'monitor',
        name: 'MonitorView',
        meta: {
          roles: ['manager', 'user']
        },
        component:MonitorView
      },
      {
        path: 'monitor/detail/:id',
        name: 'MonitorDetailView',
        meta: {
          roles: ['manager', 'user']
        },
        component:MonitorDetail
      },
      {
        path: 'monitorArea',
        name: 'MonitorAreaView',
        meta: {
          roles: ['manager']
        },
        component:MonitorAreaView
      },
      {
        path: 'modifyInfo',
        name: 'ModifyInfoView',
        meta: {
          roles: ['manager', 'user']
        },
        component:ModifyInfoView
      },
      {
        path: 'addOld',
        name: 'AddOld',
        meta: {
          roles: ['manager', 'user']
        },
        component:AddOldView
      },
      {
        path: 'addWorker',
        name: 'AddWorker',
        meta: {
          roles: ['manager', 'user']
        },
        component:AddWorkerView
      },
      {
        path: 'log',
        name: 'Log',
        meta: {
          roles: ['manager', 'user']
        },
        component:LogView
      },
      {
        path: 'trace',
        name: 'Trace',
        meta: {
          roles: ['manager', 'user']
        },
        component:TraceView
      }
      ]
  }
]
const router = new VueRouter({
  routes
})

router.beforeEach((to,from,next)=>{
  // console.log(to,from)
  // console.log(window.sessionStorage.getItem('role'))
  // if(to.name){
  //   // if(to.name === 'LoginView'||to.name === 'ManagerLoginView'||to.name === 'RegisterView'||to.meta.roles.includes(window.sessionStorage.getItem('role'))){
  //   //   console.log("aaf")
  //   //   next() //放行
  //   // }
  //
  // }else return false
  next()
})

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

export default router
